package
{
	import com.Logging.*;
	
	import flash.geom.Point;
	
	import org.flixel.FlxG;
	
	public class Logging
	{
		private var client:CGSClient;
		
		//g_code  2201
		public static const gid:int = 4;
		public static const cid:int = 7;
		public static const vid:int = 5;
		public static const g_name:String="elemental";
		
		public static const MAP_EDITOR:int = 150;
		public static const PUZZLE_LOADER:int = 250;
		public static const QUICK_PLAY:int = 300;
		public static const IN_GAME:int = 100;
		public static const PLAYER_MADE_PUZZLE:int = 200;
		
		public static const PLAYER_ROOM:int = 350;
		
		public static const TUTORIAL:int = 1000;
		
		
		private var currentLevel:int;
		
		public var uid:String;
		private var dqid:String;
		public static var time:Number = 0;
		
		private const LOGGING:Boolean = !FlxG.debug;
		
		public function Logging()
		{
			if (Elemental.FACEBOOK_MODE == Elemental.MODE){
				uid = Elemental.fbInfo.userID;
				client = new CGSClient(CGSClientConstants.URL,Logging.gid,Logging.cid,Logging.vid);
				client.message.uid = this.uid;
			}else {
				client = new CGSClient(CGSClientConstants.URL,Logging.gid,Logging.cid,Logging.vid);
				client.SetUid(function(val:String):void {
					uid = val;
				});
			}
		}
		
		public function changeUID():void{
			uid = Elemental.fbInfo.userID;
			client.message.uid = this.uid;
		}
		
		public function startDquest(level:int):void {
			if (LOGGING){
				trace("reporting new level: "+level);
				time = 0;
				currentLevel = level;
				client.SetDqid(function(val:String):void {
					dqid = val;
					client.ReportLevel(dqid,currentLevel,newLevelCallBack);
				});
			}
		}
		private function newLevelCallBack(val:String):void{
			if (val == null){
				trace("fail");
			}else{
				trace("reported new level");
				/*var action:ClientAction = new ClientAction();
				action.aid = 100;
				action.ts = 1;
				action.uid = uid;
				action.detail = new Object();
				action.detail["x"] = 100;
				action.detail["y"] = 100;
		
				client.LogAction(action);*/
			}
		}
		
		public function logArrowPlaced(index:int, arrowDirection:int):void{
			var point:Point = GameState.getPointFromIndex(index);

			if (LOGGING){
	
				var action:ClientAction = new ClientAction();
				action.aid = ClientActionType.PLACE_ARROW;
				action.ts = time; 
				action.uid = uid;
				
				action.detail = new Object();
				action.detail["x"] = point.x;
				action.detail["y"] = point.y;
				action.detail["direction"] = arrowDirection;
				
				client.LogAction(action);
			}
			trace("Arrow placed logged at "+point.x+","+point.y);
		}
		
		public function logMonsterKilled(x:Number, y:Number):void{
			if (LOGGING){
	
				var action:ClientAction = new ClientAction();
				action.aid = ClientActionType.KILL_MONSTER;
				action.ts = time; 
				action.uid = uid;
				action.detail = new Object();
				
				action.detail["x"] = x;
				action.detail["y"] = y;
				
				client.LogAction(action);
			}
			trace("monster kill logged at "+x+","+y);
		}
		
		//not used!
		public function logObstacleCrossed(player:Player):void{
			if (LOGGING){
	
				var action:ClientAction = new ClientAction();
				action.aid = ClientActionType.OBSTACLE_C;
				action.ts = time; 
				action.uid = uid;
				action.detail = new Object();
				
				action.detail["x"] = player.x;
				action.detail["y"] = player.y;
				action.detail["element"]=player.element;
				
				client.LogAction(action);
			}
			trace("player encounter obstacle logged at "+player.x+","+player.y);
		}
		
		public function logLevelClear(level:String):void{
			if (LOGGING){
	
				var action:ClientAction = new ClientAction();
				action.aid = ClientActionType.CLEAR_LV;
				action.ts = time; 
				action.uid = uid;
				
				action.detail = new Object();
				action.detail["levelClear"] = level;
				
				client.LogAction(action);
			}
			trace("level clear logged!");
		}
		
		public function logLevelFail(level:String):void{
			if (LOGGING){
	
				var action:ClientAction = new ClientAction();
				action.aid = ClientActionType.FAIL_LV;
				action.ts = time; 
				action.uid = uid;
				
				action.detail = new Object();
				action.detail["levelFail"] = level;
				
				client.LogAction(action);
			}
			trace("level fail logged!");
		}
		public function logkillByMonster(x:Number, y:Number,level:String):void{
			if (LOGGING){
				
				var action:ClientAction = new ClientAction();
				action.aid = ClientActionType.KILL_BY_MONSTER;
				action.ts = time; 
				action.uid = uid;
				action.detail = new Object();
				
				action.detail["x"] = x;
				action.detail["y"] = y;
				action.detail["levelFail"] = level;
				
				client.LogAction(action);
			}
			trace("kill by mob: "+ x+","+y + " on " + level);
		}
		public function logCheatSheet():void{
			if (LOGGING){
				var action:ClientAction = new ClientAction();
				action.aid = ClientActionType.OPEN_CHEAT;
				action.ts = time; 
				action.uid = uid;
				//action.detail = new Object();
				client.LogAction(action);
			}
			trace("log cheat sheet open");
		}
		
		public function logMapCreated(name:String):void{
			if (LOGGING){
				
				var action:ClientAction = new ClientAction();
				action.aid = ClientActionType.CREATE_MAP;
				action.ts = time; 
				action.uid = uid;
				action.detail = new Object();
				
				action.detail["newMapName"] = name;
				
				client.LogAction(action);
			}
		}
		
		
		public function logPlayerMapPlayed(name:int):void{
			if (LOGGING){
				
				var action:ClientAction = new ClientAction();
				action.aid = ClientActionType.PLAY_CUSTOM_MAP;
				action.ts = time; 
				action.uid = uid;
				action.detail = new Object();
				
				action.detail["mapName"] = name;
				
				client.LogAction(action);
			}
		}
		
		public function logEnterFriendRoom(name:String,id:String):void{
			if (LOGGING){
				
				var action:ClientAction = new ClientAction();
				action.aid = ClientActionType.ENTER_FROOM;
				action.ts = time; 
				action.uid = uid;
				action.detail = new Object();
				
				action.detail["name"] = name;
				action.detail["id"]  = id;
				
				client.LogAction(action);
			}
		}
		public function logChallenge(to:String,from:String, map:String):void{
			if (LOGGING){
				
				var action:ClientAction = new ClientAction();
				action.aid = ClientActionType.CHALLENGE;
				action.ts = time; 
				action.uid = uid;
				action.detail = new Object();
				
				action.detail["to"] = to;
				action.detail["from"] =from;
				action.detail["map"] = map;
				
				client.LogAction(action);
			}
		}
		
		public function logInvites():void{
			if (LOGGING){
				trace('loggg');
				var action:ClientAction = new ClientAction();
				action.aid = ClientActionType.INVITE;
				action.ts = time; 
				action.uid = uid;
				action.detail = new Object();
				
				client.LogAction(action);
			}
		}
	}
	
}